<?php

namespace app\mobile\controller;

use think\Db;
use think\Request;
use app\mobile\model\Ad;
use app\mobile\model\Shop;
use app\mobile\model\Brand;
use app\mobile\model\Config;
use app\mobile\model\ShopType;
use app\mobile\model\GoodsType;
use app\mobile\model\ShopGoods;
use app\mobile\model\GroupGoods;

/** * Class UserAction
 * @package app\mobile\controller
 * 用户活动板块(限时抢购&天天特价..)
 */
class UserAction extends BaseController
{
    /*     * 天天特价     */
    public function day_off_price(Request $request)
    {
//查找栏目类别
        $type = (new GoodsType())->where('count', 1)->field('id,title')->select();
        if ($request->isPost()) {
            // 获取数据
            $page = $request->post('page');
            $sale = new GroupGoods();
            $type_id = Request::instance()->param('type_id');
            $con['status'] = 1;
            $con['state'] = 1;
            if ($type_id) {
                $con['goods_str'] = ['like', '%,' . $type_id . ',%'];
            }
            $arr['data'] = Db::view('group_goods', 'goods_id,price as sale_price,status')->view('shop_goods', 'title,price,thumb_img,sale_number,rebate_percentage,goods_str', 'group_goods.goods_id = shop_goods.id')->where($con)->limit(($page - 1) * 10, 10)->select();
            //总页数
            $arr['pages'] = ceil($sale->count('id') / 10);
            return $arr;
        }
        return $this->fetch('', [
            'type' => $type
        ]);
    }

    /*
    * 热卖榜
     * */
    public function hot_sale_notice()
    {
        //获取平台商品审核状态
        $audit = (new  Config())->where('varname', 'web_commodity_audit')->value('varvalue');
        $condition = ($audit == 'Y') ? ['status' => 1, 'state' => 1] : ['state' => 1];
        $GoodsTypeModel = new GoodsType();
        $terrace = $GoodsTypeModel->where(['parenTid' => 0, 'status' => 1])->field('id,title')->limit(5)->select();
        //查找每个平台栏目对应的商品
        $model = new ShopGoods();
        foreach ($terrace as &$v) {
            $v['goods'] = $model->where('goods_str', 'like', '%,' . $v['id'] . ',%')->where($condition)->order('sale_number desc')->field('id,thumb_img,price,rebate_percentage')->limit(10)->select();
            $v['sonId'] = $GoodsTypeModel->where(['parenTid' => $v['id'], 'status' => 1])->order(['sort' => 'desc', 'update_time' => 'desc'])->value('id');
        }
        return $this->fetch('', [
            'terrace' => $terrace,
        ]);
    }

    /*     * 热卖获取更多     */
    public function get_more(Request $request, $id = '')
    {
        //获取平台商品审核状态
        $audit = (new  Config())->where('varname', 'web_commodity_audit')->value('varvalue');
        $condition = ($audit == 'Y') ? ['status' => 1, 'state' => 1] : ['state' => 1];
        //查找平台栏目
        $opt = (new GoodsType())->where(['status' => 1])->order(['sort' => 'desc'])->field('id,parenTid,title,parentStr')->select();
        $type = recursion_node($opt);
        //刚进页面时显示的栏目
        if (!$id) {
            //未传id
            if (isset($type[0]->classify)) {
                //第一个节点有子类
                $id = $type[0]->classify[0]['id'];
                //取第一个子类id
            } else {
                $id = $type[0]['id'];
                //取父类id
            }
        }
        //查找此id的上级id串
        $parentStr = GoodsType::where('id', $id)->value('parentStr');
        if ($request->isPost()) {
            $page = $request->post('page');
            $id = $request->post('id');
            //查找对应平台栏目的商品
            $model = new ShopGoods();
            $goods['data'] = $model->where(['goods_str' => ['like', '%,' . $id . ',%']])->where($condition)->order('sale_number desc')->field('id,thumb_img,price,rebate_percentage,title')->limit(($page - 1) * 10, 10)->select();
            //计算总页数
            $goods['pages'] = ceil($model->where(['goods_str' => ['like', '%,' . $id . ',%']])->where($condition)->count('id') / 10);
            return $goods;
        }
        return $this->fetch('', [
            'type' => $type,
            'id' => $id,
            'parentId' => $parentStr,
        ]);
    }

    /*     * 底层子栏目分类id     */
    public function last_level_id($arr)
    {
        if (!isset(reset($arr)['classify'])) {
            return reset($arr)['id'];
        } else {
            return self::last_level_id(reset($arr)['classify']);
        }
    }

    /*     * 品牌街     */
    public function brand_list($sort = 1)
    {
        $brand = new Brand();
        //查找品牌广告
        $category = (new Ad())->where('parenTid', 9)->field('id,picUrl,skip_type,linkUrl')->order(['sort' => 'desc', 'id' => 'desc'])->select();
        //查找推荐品牌
        $tj_brand = $brand->where('recommend', 1)->order('sort desc')->limit(8)->select();
        //按首字母查询
        $arr = $brand->where('letter', 'in', $brand->condition($sort))->where(['shop_id' => ['neq', '']])->order(['sort' => 'desc', 'update_time' => 'desc'])->limit(8)->select();
        return $this->fetch('', [
            'category' => $category,
            'tj_brand' => $tj_brand,
            'arr' => $arr,
            'sort' => $sort,
        ]);
    }

    /*     * 热门市场     */
    public function hot_market()
    {
        $bigPic = Ad::where(['parenTid' => '6'])->field('linkUrl,picUrl,skip_type')->limit(2)->select();
        $type_list = GoodsType::where(['parenTid' => '0', 'hot_recommend' => '1', 'status' => '1'])->field('id,title,picUrl')->order(['sort' => 'desc'])->select();

        return $this->fetch('', [
            'bigPic' => $bigPic,
            'type_list' => $type_list,
        ]);
    }

    /*     * 热门市场子类     */
    public function market_category($type_id)
    {
        // banner列表
        $banner = Ad::where(['parenTid' => '19'])->field('linkUrl,picUrl,skip_type')->find();
        // 商品分类
        $type = GoodsType::where(['parentStr' => ['like', '%,' . $type_id . ',%']])->field('id,title,parenTid')->select();
        $son_id = Request::instance()->get('son_id');
        //查找此id的上级id串
        $parentStr = GoodsType::where('id', $son_id)->value('parentStr');
        if (Request::instance()->isPost()) {
            // 搜索条件
            $condition['state'] = ['eq', 1];
            $condition['activity'] = ['eq', 2];
            // $condition['shop_recommend'] = ['eq', 1];
            // 是否开启审核
            if ((new Config())->where('varname', 'web_commodity_audit')->value('varvalue') == 'Y') $condition['status'] = ['eq', '1'];
            // 条件
            $id = Request::instance()->post('son_id');
            $type_id = Request::instance()->post('type_id');
            $page = Request::instance()->post('page');
            $condition['goods_str'] = ['like', '%,' . $type_id . ',%'];
            if ($id) $condition['goods_str'] = ['like', '%,' . $id . ',%'];
            // 商品列表
            $data['data'] = ShopGoods::where($condition)->field('id,title,price,thumb_img,rebate_percentage,sale_number,market_price')->order(['sort' => 'desc'])->limit(($page - 1) * 10, 10)->select();
            $data['pages'] = ceil(ShopGoods::where($condition)->count() / 10);
            return $data;
        }
        $empty = '<p style="text-align: center;color: #727272;margin-top: 2%">无更多分类</p>';
        //大分类名称
        $type_name = GoodsType::where('id', $type_id)->value('title');
        return $this->fetch('', [
            'banner' => $banner,
            'type' => recursion_node($type, $type_id),
            'parentId' => explode(',', $parentStr),
            'empty' => $empty,
            'type_name' => $type_name,
        ]);
    }

    // 发现好店
    public function find_gooder($type_id = 'recommend')
    {
        // 实例化
        $shop_type_model = new ShopType();
        $model = new Shop();
        $ad_model = new Ad();
        //店铺分类
        $shop_type = $shop_type_model->where('status', 1)->select();
        if ($type_id == 'recommend') {
            $condition = [];
        } elseif ($type_id == 'all') {
            $condition = [];
        } else {
            $condition['type_id'] = $type_id;
        }
        // 筛选条件
        $condition['status'] = ['eq', 1];
        $condition['recommend'] = ['eq', 1];
        $condition['valid_time'] = [['gt', time()], ['eq', ''], 'or'];
        if (Request::instance()->isPost()) {
            // 获取页码
            $page = Request::instance()->post('page');
            // 获取数据
            $arr['data'] = $model->where($condition)->order(['recommend' => 'asc', 'sort' => 'desc', 'create_time' => 'desc'])->relation(['shopGoods' => function ($query) {
                $query->limit(4)->order(['sort' => 'desc', 'create_time' => 'desc'])->field('id,member_id,thumb_img,price');
            }])->field('member_id,title,m_banner banner,description,collect_number,type')->limit(($page - 1) * 10, 10)->select();
            if ($arr['data']) {
                foreach ($arr['data'] as &$value) {
                    $num = $value['collect_number'];
                    if ($num / 1000 >= 1 && $num <= 10) {
                        $value['collect_number'] = ($num / 1000) . '千';
                    } elseif ($num / 10000 >= 1 && $num / 10000 <= 10) {
                        $value['collect_number'] = ($num / 10000) . '万';
                    }
                }
            }
            $arr['pages'] = ceil($model->where($condition)->count('member_id') / 10);
            return $arr;
        }
        // 广告位
        $ad = $ad_model->where('parenTid', 17)->find();
        return $this->fetch('', [
            'ad' => $ad,
            'type' => $type_id,
            'shop_type' => $shop_type,
        ]);
    }
}